package com.xjgc.eip.twice.zxjeip2cwdjcss.controller;


import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjgc.common.constant.Consants;
import com.xjgc.common.entity.ReturnResult;
import com.xjgc.common.enums.ReturnStatus;
import com.xjgc.common.utils.BetValUtil;
import com.xjgc.eip.twice.zxjeip2cwdjcss.entity.ZxjEip2cWdjcss;
import com.xjgc.eip.twice.zxjeip2cwdjcss.service.ZxjEip2cWdjcssService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XXX
 * @since 2019-12-06
 */
@Api("波峰焊回流焊温度实时监控信息抓取接口")
@RestController
@RequestMapping("/apis/zxjEip2cWdjc")
public class ZxjEip2cWdjcssController {
    @Autowired
    ZxjEip2cWdjcssService zxjEip2cWdjcssService;
    /**
     * 波峰焊回流焊温度实时监控信息抓取接口
     */
    @ApiOperation("波峰焊回流焊温度实时监控信息抓取接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "amount", value = "查询条数",
                    required = true, dataType = "int")
    })
    @GetMapping(value = "/getData")
    @ResponseBody
    public ReturnResult getData(Integer amount,@RequestHeader(name = "supplierCode",required = false)String supplierCode) {
        ReturnResult returnResult = new ReturnResult();
        QueryWrapper<ZxjEip2cWdjcss> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("ioflag", Consants.Unread)
                .last("limit " + (amount == null ? 100 : amount));
        if(StringUtils.isNotBlank(supplierCode)){
            queryWrapper.eq("supplier_code",supplierCode);
        }
        try {
            List<ZxjEip2cWdjcss> result = zxjEip2cWdjcssService.list(queryWrapper);
            //更新查询的数据为已读
            if (result != null && result.size() > 0) {
                for (ZxjEip2cWdjcss zed : result) {
                    zed.setIoflag(Consants.read);
                    zed.setUpdateTime(new Date());
                }
                if (zxjEip2cWdjcssService.updateBatchById(result)) {
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.OK.getCode()));
                    returnResult.setData(result);
                } else {
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
                    returnResult.setMessage("请求异常");
                }
            }
        } catch (Exception e) {
            returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
            returnResult.setMessage("请求异常");
        }
        return returnResult;
    }
    @GetMapping(value = "/getJson")
    public ZxjEip2cWdjcss gt(){
        ReturnResult returnResult = this.getData(Integer.valueOf(1),"");
        returnResult.getData();
        JSONArray jsonArray= JSONArray.fromObject(returnResult.getData());
        Object o=jsonArray.get(0);
        JSONObject jsonObject2= JSONObject.fromObject(o);
        ZxjEip2cWdjcss wdjcss = (ZxjEip2cWdjcss)JSONObject.toBean(jsonObject2,ZxjEip2cWdjcss.class);
        System.out.println(wdjcss.toString());
        return wdjcss;
    }
    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    @DS("proxyDs")
    public Result<?> queryPageList(ZxjEip2cWdjcss zxjEip2cWdjcss,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) throws ParseException {
        /*QueryWrapper<ZxjEip2cWdjcss> queryWrapper = QueryGenerator.initQueryWrapper(zxjEip2cWdjcss, req.getParameterMap());//实体、数据库字段大小写问题无法使用此方法*/
        QueryWrapper<ZxjEip2cWdjcss> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(zxjEip2cWdjcss);

        //区间查询
        Map<String, String> map = BetValUtil.betValue(zxjEip2cWdjcss, req.getParameterMap());
        if(map.containsKey("collect_begin")){
            queryWrapper.ge("collect_time",BetValUtil.parseDate(map.get("collect_begin")));
        }
        if (map.containsKey("collect_end")){
            queryWrapper.le("collect_time",BetValUtil.parseDate(map.get("collect_end")));
        }

        //
        /*if(map.containsKey("boardNo")){
            queryWrapper.eq("smt_bkbh",map.get("boardNo"));
        }*/
        Page<ZxjEip2cWdjcss> page = new Page<ZxjEip2cWdjcss>(pageNo, pageSize);
        IPage<ZxjEip2cWdjcss> pageList = zxjEip2cWdjcssService.page(page, queryWrapper);
        return Result.ok(pageList);
    }
}

